<template>
  <div class="box-main1">
    <div class="box-header2">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/admin/index' }">控制台</el-breadcrumb-item>
        <el-breadcrumb-item>布置作业信息</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="box-main2">
      <div class="box-search2">

        <div class="box-input2" style="width: 180px">
          <el-input  @keydown.enter.native="clkBtnSearch" @clear="clkBtnSearch" clearable
                     size="mini" v-model="searchInfo.searchKey" placeholder="作业名称"></el-input>
        </div>
        <div class="box-input2">
          <el-button size="mini" type="primary" icon="el-icon-search" @click="clkBtnSearch">搜索</el-button>
          <el-button size="mini" type="warning" icon="el-icon-plus" @click="clkBtnAdd">新增</el-button>
        </div>

      </div>

      <div class="box-content2">
        <div class="box-table2">
          <el-table class="tbl1" :data="buzhiZuoyePage.list" :stripe="true" size="small">

            <el-table-column type="index" :index="1" label="序号" width="55"></el-table-column>
            <el-table-column label="作业名称" width="180">
              <template slot-scope="scope">
                <span >{{scope.row.zuoye_title}}</span>
              </template>
            </el-table-column>
            <el-table-column label="作业详情">
              <template slot-scope="scope">
                <span >{{scope.row.zuoye_detail|filterMultiText}}</span>
              </template>
            </el-table-column>
            <el-table-column label="布置日期" width="120">
              <template slot-scope="scope">
                <span >{{scope.row.buzhi_date|filterDateFormat1}}</span>
              </template>
            </el-table-column>

            <el-table-column label="创建人" width="100">
              <template slot-scope="scope">
                <span>{{scope.row.createUserRealname}}</span>
              </template>
            </el-table-column>

            <el-table-column  label="操作" width="180">
              <template slot-scope="scope">
                 <span>
                   <el-button type="success" size="mini" @click="clkBtnRefresh(scope.row )">刷新学生作业</el-button>
                   <el-button type="primary" size="mini" @click="clkBtnEdit(scope.row )">编辑</el-button>
                   <el-button type="warning" size="mini" @click="clkBtnDelete(scope.row )">删除</el-button>
                 </span>
              </template>
            </el-table-column>

          </el-table>
        </div>
        <div class="box-page2">
          <el-pagination layout="prev, pager, next" :total="buzhiZuoyePage.total"
                         :current-page="buzhiZuoyePage.pageNum" :page-size="buzhiZuoyePage.pageSize"
                         @current-change="chgCurrPage">
          </el-pagination>
        </div>
      </div>
    </div>


    <el-dialog title="新增" class="dialog1"
               :show-close="false" :close-on-click-modal="false"
               :visible.sync="showAddBuzhiZuoyeDialog">
      <div>
        <div class="box-content2">
          <div class="box-table2" >
            <el-form size="mini" ref="student" :model="buzhiZuoye" label-width="120px">
              <el-form-item label="作业标题">
                <el-input v-model="buzhiZuoye.zuoye_title"></el-input>
              </el-form-item>
              <el-form-item label="作业详细内容">
                <el-input type="textarea" rows="4" v-model="buzhiZuoye.zuoye_detail"></el-input>
              </el-form-item>
              <el-form-item label="布置时间">
                <el-date-picker v-model="buzhiZuoye.buzhi_date" format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss"
                  type="datetime" placeholder="选择日期">
                </el-date-picker>
              </el-form-item>

            </el-form>
          </div>

        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="clkBtnSave">确 定</el-button>
        <el-button size="mini" @click="showAddBuzhiZuoyeDialog = false">取 消</el-button>
      </span>
    </el-dialog>


    <el-dialog title="编辑" class="dialog1"
               :show-close="false" :close-on-click-modal="false"
               :visible.sync="showUpdateStudentDialog">
      <div>
        <div class="box-content2">
          <div class="box-table2" >
            <el-form size="mini" ref="student" :model="buzhiZuoye" label-width="120px">
              <el-form-item label="作业标题">
                <el-input v-model="buzhiZuoye.zuoye_title"></el-input>
              </el-form-item>
              <el-form-item label="作业详细内容">
                <el-input type="textarea" rows="4" v-model="buzhiZuoye.zuoye_detail"></el-input>
              </el-form-item>
              <el-form-item label="布置时间">
                <el-date-picker v-model="buzhiZuoye.buzhi_date" format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetime" placeholder="选择日期">
                </el-date-picker>
              </el-form-item>

            </el-form>
          </div>

        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="clkBtnEditRight">确 定</el-button>
        <el-button size="mini" @click="showUpdateStudentDialog = false">取 消</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
  import request from '@/common/utils/request';

  export default {
    computed: {
      loginUserInfo() {
        return this.$store.state.loginUserInfo;
      },

    },
    data() {
      return {
        searchInfo: {searchKey: null},
        buzhiZuoyePage: {pageNum:1 ,pageSize:12, list:[] },

        pageLoading: false,

        showAddBuzhiZuoyeDialog: false,
        showUpdateStudentDialog: false,
        buzhiZuoye: {},

      };
    },
    mounted() {
      this._initData();
    },
    watch: {
      $route: {
        handler: function (to, from) {
          if (to.path == '/admin/buzhi_zuoye_list') {

          }

        }, immediate: true
      },
    },
    methods: {
      _initData() {
        this.getList();
      },

      clkBtnSearch() {
        this.getList();
      },

      getList() {
        let url1 = this.settings.apiUrl + '/buzhi_zuoye/page';
        let d1 = {};
        d1.pageNum = this.buzhiZuoyePage.pageNum;
        d1.pageSize = this.buzhiZuoyePage.pageSize;

        d1.searchKey = this.searchInfo.searchKey;

        request.post(url1, d1).then((d1r) => {
          this.pageLoading = false;
          if (d1r.code === 0) {
            this.buzhiZuoyePage = d1r.data;
          } else {

            this.$alert('获取布置作业信息失败：' + d1r.msg, '提示', {
              type: 'warning'
            });

          }
        }).catch(err => {
          this.pageLoading = false;
        });
      },
      // 点击新增用户的按钮
      clkBtnAdd() {
        this.buzhiZuoye = {};
        this.showAddBuzhiZuoyeDialog = true;
      },
      clkBtnEdit(row) {
        this.buzhiZuoye = JSON.parse(JSON.stringify(row));
        this.showUpdateStudentDialog = true;
      },
      clkBtnDelete(row) {
          let url1 = this.settings.apiUrl + '/buzhi_zuoye/delete';
          let d1 = {noid: row.noid};
          request.post(url1, d1).then((d1r) => {
            if(d1r.code == 0){
              this.getList();
              this.$message("您删除成功了");
            }else {
              this.$message("删除失败" + d1r.msg + "，提示", {
                type: 'warning'
              });
            }
          })
      },
      //编辑信息
      clkBtnEditRight(){
        let url1 = this.settings.apiUrl + '/buzhi_zuoye/update';
        let d1 = this.buzhiZuoye;
        request.post(url1 , d1).then((d1r) => {
          if(d1r.code === 0){
              //记录密码
            this.getList();
            this.showUpdateStudentDialog = false;
            this.$message("信息修改成功~");
          }else{
            this.$alert('编辑用户信息失败：'+d1r.msg,'提示', {
              type: 'warning'
            });
          }
        })
      },
      clkBtnRefresh(row){
        let url1 = this.settings.apiUrl + '/student_zuoye/refresh_by_buzhi_zuoye_id';
        let d1 = {'buzhi_zuoye_id': row.noid };

        request.post(url1, d1).then((d1r) => {
          if (d1r.code === 0) {
            this.$message.info('刷新成功', '提示', {
              type: 'success'
            });
          } else {

            this.$alert('获取布置作业信息失败：' + d1r.msg, '提示', {
              type: 'warning'
            });

          }
        }).catch(err => {
        });
      },

      clkBtnSave() {

        // 1 首先做验证
        let url1 = this.settings.apiUrl + '/buzhi_zuoye/add';
        let d1 = this.buzhiZuoye;
        d1.create_user_id = this.loginUserInfo.noid;

        request.post(url1, d1).then((d1r) => {
          if (d1r.code === 0) {
            // 记录密码
            this.getList();
            this.showAddBuzhiZuoyeDialog = false;
            this.clkBtnRefresh();
          } else {
            this.$alert('新增布置作业失败：' + d1r.msg, '提示', {
              type: 'warning'
            });
          }
        }).catch(err => {
          this.loading = false;
          console.log(err);
        });

        this.$route.go(0);

      },
      chgCurrPage(val){
        this.buzhiZuoyePage.pageNum = val;
        this.getPage();
      },
      getPage(){
        this.getList();
      }
    },
    filters:{
      filterSex(srcVal){
        if(srcVal==1){
          return '男';
        }
        if(srcVal==2){
          return '女';
        }
        return '未知';
      },
      filterDateFormat1(srcVal){
        if(!srcVal){
          return "";
        }
        if(srcVal.length<16){
          return srcVal;
        }
        return srcVal.substr(0,16);
      },
      filterMultiText(srcVal){

        return srcVal;
      }
    }

  };
</script>

<style lang="scss" scoped="scoped">

  .box-main2 {
    height: 100%;
    background-repeat: no-repeat;
    background-size: cover;
  }

  .box-header2{
    height: 30px;
    background-color: #eceeef;
    padding-left:20px;
    padding-top:8px;

  }

  .box-search2{
    display: flex;
  }
  .box-page2{
    display: flex;
    justify-content: flex-end;
  }

  .box-search2{
    padding-bottom: 10px;
    .box-input2{
      padding-right:10px;
    }
  }



</style>
